package com.jsyh.maidu.activity.me;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.BaseActivity;
import com.jsyh.maidu.adapter.BonusListAdapter;
import com.jsyh.maidu.config.SPConfig;
import com.jsyh.maidu.model.BaseModel;
import com.jsyh.maidu.model.Bonus;
import com.jsyh.maidu.model.BonusModel;
import com.jsyh.maidu.presenter.MeBounsPresenter;
import com.jsyh.maidu.utils.SPUtils;
import com.jsyh.maidu.views.MeBounsView;

import java.util.List;


public class MeBounsActivity extends BaseActivity implements MeBounsView, View.OnClickListener {

    private RecyclerView bonusList;
    private MeBounsPresenter presenter;
    private TextView addBonus;
    private EditText bonusEdit;

    private String key;
    private int page;

    private List<Bonus> datas;
    private BonusListAdapter adapter;
    private LinearLayoutManager layoutManager;

    private TextView title;
    private FrameLayout back;

    private String bonusNumber;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_mebouns);
        bonusList = (RecyclerView) findViewById(R.id.bonus_list);
        addBonus = (TextView) findViewById(R.id.add_bonus);
        bonusEdit = (EditText) findViewById(R.id.bonus_search_edit);

        layoutManager = new LinearLayoutManager(this);
        bonusList.setLayoutManager(layoutManager);

        addBonus.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        key = (String) SPUtils.get(this, SPConfig.KEY, "");
        presenter = new MeBounsPresenter(this);
        page = 1;
        presenter.getBonusList(this, key, String.valueOf(page));
    }

    @Override
    public void initTitle() {
        super.initTitle();
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        title.setText("我的红包");
        back.setOnClickListener(this);
    }

    @Override
    public void getBonus(BonusModel model) {
        if (model.getCode().equals("1")) {
            datas = model.getData();
            adapter = new BonusListAdapter(datas, this);
            bonusList.setAdapter(adapter);
        }

    }

    @Override
    public void getAddResult(BaseModel model) {
        if (model.getCode().equals("1")){
            Toast.makeText(this, model.getMsg(), Toast.LENGTH_SHORT).show();
            presenter.getBonusList(this, key, String.valueOf(page));
        }else {
            Toast.makeText(this, model.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_Left:
                finish();
                break;
            case R.id.add_bonus:
                bonusNumber = bonusEdit.getText().toString().trim();
                if (TextUtils.isEmpty(bonusNumber)){
                    Toast.makeText(this, "请输入红包序列号", Toast.LENGTH_SHORT).show();
                }else {
                    presenter.addBonus(this, key, bonusNumber);
                }
                break;
        }
    }
}
